package com.company.genealogy.web.service.impl;

import com.company.genealogy.web.service.FileUploadService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Value("${upload.path:/uploads}")
    private String uploadPath;

    @Value("${upload.max-size:5242880}")
    private long maxFileSize; // 5MB default

    @Override
    public String uploadMemberPhoto(MultipartFile file) throws IOException {
        // 验证文件类型
        String contentType = file.getContentType();
        if (contentType == null || !contentType.startsWith("image/")) {
            throw new IllegalArgumentException("只能上传图片文件");
        }

        // 验证文件大小
        if (file.getSize() > maxFileSize) {
            throw new IllegalArgumentException("文件大小不能超过5MB");
        }

        // 创建上传目录
        String uploadDir = uploadPath + "/member-photos";
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        // 生成文件名
        String originalFilename = file.getOriginalFilename();
        String extension = "";
        if (originalFilename != null && originalFilename.contains(".")) {
            extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        
        String fileName = UUID.randomUUID().toString() + extension;
        String datePath = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        String relativePath = "/member-photos/" + datePath + "/" + fileName;
        
        // 创建日期目录
        File dateDir = new File(uploadDir + "/" + datePath);
        if (!dateDir.exists()) {
            dateDir.mkdirs();
        }

        // 保存文件
        Path filePath = Paths.get(uploadDir, datePath, fileName);
        Files.copy(file.getInputStream(), filePath);

        return relativePath;
    }

    @Override
    public String uploadSpousePhoto(MultipartFile file) throws IOException {
        // 验证文件类型
        String contentType = file.getContentType();
        if (contentType == null || !contentType.startsWith("image/")) {
            throw new IllegalArgumentException("只能上传图片文件");
        }

        // 验证文件大小
        if (file.getSize() > maxFileSize) {
            throw new IllegalArgumentException("文件大小不能超过5MB");
        }

        // 创建上传目录
        String uploadDir = uploadPath + "/spouse-photos";
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        // 生成文件名
        String originalFilename = file.getOriginalFilename();
        String extension = "";
        if (originalFilename != null && originalFilename.contains(".")) {
            extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        
        String fileName = UUID.randomUUID().toString() + extension;
        String datePath = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        String relativePath = "/spouse-photos/" + datePath + "/" + fileName;
        
        // 创建日期目录
        File dateDir = new File(uploadDir + "/" + datePath);
        if (!dateDir.exists()) {
            dateDir.mkdirs();
        }

        // 保存文件
        Path filePath = Paths.get(uploadDir, datePath, fileName);
        Files.copy(file.getInputStream(), filePath);

        return relativePath;
    }

    @Override
    public boolean deleteFile(String filePath) {
        try {
            if (filePath != null && !filePath.isEmpty()) {
                Path fullPath = Paths.get(uploadPath, filePath);
                if (Files.exists(fullPath)) {
                    Files.delete(fullPath);
                    return true;
                }
            }
            return false;
        } catch (IOException e) {
            return false;
        }
    }
} 